const {
  MemberModel
} = require('../../../../utils/member-model.js');
var time = require('../../../../utils/util.js');
var memberModel = new MemberModel();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keyType: "", //修改的key[memberName(姓名),birthday(生日),idNumber(身份证),,email(绑定邮箱)]
    keyValue: "", //修改的值
    disabled: false, //是否禁用
    endTime: "",
    defaultDate: '1990-01-01',
    isfocus: false,
    navTitle: "个人资料",
    abcd: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var dateTime = time.formatTime(new Date()).replace(/(\/)/g, "-").substr(0, 11); //获取当前时间
    this.setData({
      keyType: options.keyType || "",
      keyValue: options.keyValue || "",
      disabled: options.keyValue ? false : true,
      endTime: dateTime,
      navTitle: options.keyType == "memberName" ? "姓名" : options.keyType == "birthday" ? "生日" : options.keyType == "idNumber" ? "身份证" : options.keyType == "email" ? "绑定邮箱" : "",
    }, function() {
      wx.setNavigationBarTitle({
        title: this.data.navTitle
      })
    });
  },
  /**
   * 日期选择
   */
  bindDateChange: function(e) {
    var type = true;
    this.setData({
      keyValue: e.detail.value
    }, function() {
      if (this.data.keyValue == "") {
        type = true;
      } else {
        type = false;
      }
      this.setData({
        disabled: type
      });
    })
  },

  /**
   * 清除信息
   */
  clearInfo: function() {
    this.setData({
      isfocus: false,
    }, function() {
      this.setData({
        keyValue: "",
        disabled: true,
      })
    })
  },
  /**
   * 修改个人信息
   * key memberName(姓名),gender(性别),birthday(生日),idNumber(身份证),education(教育背景),city(常住城市),email(绑定邮箱),familyInfo(家庭信息)
   */
  modifyPersonalInformation: function() {
    var param = {}
    param.merchantId = wx.getStorageSync('merchantId');
    param.key = this.data.keyType;
    param.value = this.data.keyValue;
    memberModel.userUpdate(param).then(data => {
      wx.navigateBack({
        delta: 1
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },
  /**
   * 参数验证
   * */
  checkInfo: function(e) {
    var value = e.detail.value;
    this.setData({
      keyValue: value
    }, function() {
      var type = this.data.keyType;
      var t = false;
      var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/; //身份证验证
      var myreg = /^([\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/; //邮箱验证
      var name = "^[a-zA-Z\u4e00-\u9fa5]+$";
      var re = new RegExp(name); //姓名验证
      if (type == "memberName") {
        if (this.data.keyValue == "") {
          t = true;
        } else {
          if (!re.test(this.data.keyValue)) {
            t = true;
            wx.showToast({
              title: `抱歉您输入的格式错误`,
              icon: 'none',
              duration: 2000
            })
          }
        }
      } else if (type == "idNumber") {
        if (this.data.keyValue == "") {
          t = true;
        } else if (!reg.test(this.data.keyValue)) {
          t = true;
        }
      } else if (type == "email") {
        if (this.data.keyValue == "") {
          t = true;
        } else if (!myreg.test(this.data.keyValue)) {
          t = true;
          // wx.showToast({
          //   title: `您输入的邮箱格式错误`,
          //   icon: 'none',
          //   duration: 2000
          // })
        }
      } else if (type == "birthday") {
        if (this.data.keyValue == "") {
          t = true;
        }
      }
      this.setData({
        disabled: t
      })
    })

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    this.setData({
      keyType: "",
      keyValue: "",
    })
  },
})